package com.example.filterdemo.config;

import com.example.filterdemo.filter.RequestTimingFilter;
import com.example.filterdemo.filter.Test1Filter;
import com.example.filterdemo.filter.Test2Filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import javax.servlet.DispatcherType;

@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean<Test1Filter> test1FilterRegistration(Test1Filter filter) {
        FilterRegistrationBean<Test1Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter(filter);
        registration.addUrlPatterns("/*");
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 2);
        registration.setName("test1Filter");
        registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD);
        return registration;
    }
    @Bean
    public FilterRegistrationBean<Test2Filter> test2FilterRegistration(Test2Filter filter) {
        FilterRegistrationBean<Test2Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter(filter);
        registration.addUrlPatterns("/*");
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
        registration.setName("test2Filter");
        registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD);
        return registration;
    }

    @Bean
    public FilterRegistrationBean<RequestTimingFilter> requestTimingFilterRegistration(
            RequestTimingFilter filter
    ) {
        FilterRegistrationBean<RequestTimingFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(filter);
        registration.addUrlPatterns("/*"); // 过滤所有路径
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE); // 最高优先级
        registration.setName("requestTimingFilter");
        registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD);
        return registration;
    }
}